from app import db
from datetime import datetime


class LoggerInfo(db.Model):
    __tablename__ = 'logger_info'

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    type = db.Column(db.String(100), nullable=True)
    title = db.Column(db.String(255), nullable=True)
    location = db.Column(db.String(1000), nullable=True)
    info = db.Column(db.String(1000), nullable=True)
    ip = db.Column(db.String(100), nullable=True)
    host = db.Column(db.String(100), nullable=True)
    ua = db.Column(db.String(1000), nullable=True)
    domain = db.Column(db.String(100), nullable=True)
    url = db.Column(db.String(1000), nullable=True)
    device = db.Column(db.String(255), nullable=True)
    remark = db.Column(db.String(255), nullable=True)
    create_time = db.Column(db.DateTime, default=datetime.utcnow)
    access = db.Column(db.Integer, default=0)

    __table_args__ = (
        db.Index('idx-ip', 'ip'),
        db.Index('idx-txt', 'title'),
        db.Index('idx-type', 'type'),
        db.Index('idx-info', 'info'),
    )

    def to_json(self):
        return {
            'id': self.id,
            'type': self.type,
            'title': self.title,
            'location': self.location,
            'info': self.info,
            'remark': self.info,
            'ip': self.ip,
            'host': self.host,
            'ua': self.ua,
            'domain': self.domain,
            'url': self.url,
            'device': self.device,
            'create_time': self.create_time.isoformat(),
            'access': self.access,
        }
